package com.allen.app.zdemo.service;

import com.alibaba.fastjson.JSONObject;
import com.allen.app.common.config.dingtalk.DingTalkHelper;
import com.allen.app.common.config.other.BusinessException;
import com.allen.app.common.utils.Jackson;
import com.allen.app.common.utils.Try;
import com.allen.app.zdemo.domain.DelayExchange;
import com.allen.app.zdemo.mq.RabbitMqHelper;
import com.allen.app.zdemo.mq.RabbitmqDatas;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MqConsumer {

    /**
     * 普通队列消息打印测试.
     *
     * @param getMessage getMessage
     * @author Limiaojun
     * @date 2020/1/10 12:18
     */
    @RabbitHandler
    @RabbitListener(queues = "xxx-server.test-mq-normal-exchange.test-mq.receive")
    public void normalPrint(String getMessage) {
        //需要捕获tryCatch异常 否则会导致队列死循环消费
        try {
            log.debug("MqConsumer.normalPrint:\t {}", getMessage);
        } catch (Exception e) {
            DingTalkHelper.send(
                new BusinessException("xxx-server.test-mq-normal-exchange.test-mq.receive",getMessage, e)
            );
        }
    }

    /**
     * 延时队列消息打印测试(不同时间会堵塞).
     *
     * @param getMessage getMessage
     * @author Limiaojun
     * @date 2020/1/10 12:18
     */
    @RabbitHandler
    @RabbitListener(queues = "xxx-server.test-mq-delay-exchange.test-mq.receive")
    public void dlxPrint(String getMessage) {
        try {
            log.debug("MqConsumer.dlxPrint:\t {}", getMessage);
        } catch (Exception e) {
            DingTalkHelper.send(
                new BusinessException("xxx-server.test-mq-delay-exchange.test-mq.receive",getMessage, e)
            );
        }
    }

    /**
     * 新延时队列消息打印测试(支持延迟不同时间).
     *
     * @param getMessage getMessage
     * @author Limiaojun
     * @date 2020/1/10 12:18
     */

    @RabbitHandler
    @RabbitListener(queues = "xxx-server.test-mq-plugs-delay-exchange.test-mq.receive")
    public void dlxPlugsPrint(String getMessage) {
        JSONObject jsonObject = Jackson.jsonObject(getMessage);

        boolean errFlag = false;
        try {
            log.debug("MqConsumer.dlxPrint.plugs:\t {}", getMessage);
        } catch (Exception e) {
            errFlag = true;
            DingTalkHelper.send(
                new BusinessException("xxx-server.test-mq-plugs-delay-exchange.test-mq.receive",getMessage, e)
            );
        } finally {
            RabbitMqHelper.deleteOrUpdateByUnionCondition(
                new RabbitmqDatas(DelayExchange.TEST_MQ_PLUGS_DLX.getExchangeName(),
                    DelayExchange.DelayQueues.TEST_MQ_PLUGS_DLX_QUEUE.getForwardName(),
                    jsonObject.get("id")),
                errFlag);
        }
    }



    /**
     * mq队列事件通知1.
     *
     * @param getMessage getMessage
     * @author Limiaojun
     * @date 2020/1/10 14:19
     */
    @RabbitHandler
    @RabbitListener(queues = "xxx-server.test-mq-event.publish1.receive")
    public void publish1Print(String getMessage) {
        Try.tryCatch(() -> {
            log.debug("MqConsumer.publish1Print:\t {}", getMessage);
        });

    }

    /**
     * mq队列事件通知2.
     *
     * @param getMessage getMessage
     * @author Limiaojun
     * @date 2020/1/10 14:19
     */
    @RabbitHandler
    @RabbitListener(queues = "xxx-server.test-mq-event.publish2.receive")
    public void publish2Print(String getMessage) {
        Try.tryCatch(() -> {
            log.debug("MqConsumer.publish2Print:\t {}", getMessage);
        });

    }
}
